package com.hhkj.study.admin.config.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.study.admin.article.entity.ArticleBanner;
import com.hhkj.study.admin.config.entity.ConfigWelcome;
import com.hhkj.study.admin.config.excel.ConfigWelcomeExcelRes;
import com.hhkj.study.admin.config.model.podo.ConfigWelcomeDo;
import com.hhkj.study.admin.config.model.podo.ConfigWelcomePo;
import com.hhkj.study.admin.config.model.req.ConfigWelcomeReq;
import com.hhkj.study.admin.config.model.res.ConfigWelcomeRes;
import com.hhkj.study.admin.config.service.base.ConfigWelcomeService;
import com.hhkj.study.admin.config.service.biz.ConfigWelcomeBizService;
import com.hhkj.study.admin.config.service.extend.ConfigWelcomeExtendService;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* ConfigWelcomeBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ConfigWelcomeBizServiceImpl extends BaseBizServiceImpl<ConfigWelcome, ConfigWelcomeReq, ConfigWelcomeRes, ConfigWelcomeDo, ConfigWelcomePo, ConfigWelcomeService, ConfigWelcomeExtendService> implements ConfigWelcomeBizService {
    @Override
    public boolean updatePublishFlag(ConfigWelcomeReq req) {
        ConfigWelcome configWelcome = this.getExtendService().getByIdNotNull(req.getId());
        if (configWelcome.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<ConfigWelcome> configWelcomeUpdate = new LambdaUpdateWrapper<>();
        configWelcomeUpdate.eq(ConfigWelcome::getId, req.getId())
                .set(ConfigWelcome::getPublishedFlag, req.getPublishedFlag());
        return this.getService().update(configWelcomeUpdate);
    }

    @Override
    public void export(ConfigWelcomeReq req, HttpServletResponse response) {
        List<ConfigWelcomeRes> welcomeList = list(req);
        if (CollectionUtil.isEmpty(welcomeList)) {
            throw new GlobalException("没有数据");
        }
        List<ConfigWelcomeExcelRes> welcomeExcelResList = BeanUtil.copyToList(welcomeList, ConfigWelcomeExcelRes.class);
        ExcelUtils.exportExcelRuoYi(response, welcomeExcelResList, ConfigWelcomeExcelRes.class, "宣传语", "宣传语列表");
    }

    @Override
    public boolean importWelcome(MultipartFile file, HttpServletRequest request) {
        List<ConfigWelcomeExcelRes> configWelcomeList = ExcelUtils.importExcelRuoYi(file, ConfigWelcomeExcelRes.class, 1);
        if (CollectionUtil.isEmpty(configWelcomeList)) {
            return false;
        }
        List<ConfigWelcome> list = BeanUtil.copyToList(configWelcomeList, ConfigWelcome.class);
        return this.getService().saveBatch(list);
    }
}